package com.zlzlib.manager.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;
import java.util.List;

/**
 * @DateTime: 2023/6/2 9:10
 * @Author zlz
 * @Version 1.0
 */
@EnableOpenApi
@EnableSwagger2
@EnableKnife4j
public abstract class BaseSwaggerConfig {

    /**
     * 是否启用swagger
     *
     * @return 是否
     */
    protected abstract boolean enableSwagger();

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .enable(enableSwagger())
                .select()
                //指定扫描的生成的api注解
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    protected List<SecurityScheme> securitySchemes() {
        return Collections.singletonList(new ApiKey("token", "token", "header"));
    }

    protected List<SecurityContext> securityContexts() {
        return Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .operationSelector(null)
                        .build()
        );
    }

    protected List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global",
                "accessEverything");
        return Collections.singletonList(new SecurityReference("token",
                new AuthorizationScope[]{authorizationScope}));
    }

    protected ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //文档名
                .title("接口文档")
                //文档描述
                .description("接口文档")
                //用户
                .contact(new Contact("zlz", "", ""))
                //文档版本
                .version("1.0")
                .build();
    }

}
